package org.dromara.basic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.dromara.basic.domain.AcademicYear;
import org.dromara.basic.domain.bo.AcademicYearBo;
import org.dromara.basic.domain.vo.AcademicYearVo;
import org.dromara.basic.mapper.AcademicYearMapper;
import org.dromara.basic.service.IAcademicYearService;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.pe.domain.TaskInfo;
import org.dromara.pe.domain.vo.TaskInfoVo;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 学年管理Service业务层处理
 *
 * @author Lion Li
 * @date 2024-11-14
 */
@RequiredArgsConstructor
@Service
public class AcademicYearServiceImpl implements IAcademicYearService {

    private final AcademicYearMapper baseMapper;

    /**
     * 查询学年管理
     */
    @Override
    public AcademicYearVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询学年管理列表
     */
    @Override
    public TableDataInfo<AcademicYearVo> queryPageList(AcademicYearBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<AcademicYear> lqw = buildQueryWrapper(bo);
        Page<AcademicYearVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询学年管理列表
     */
    @Override
    public List<AcademicYearVo> queryList(AcademicYearBo bo) {
        LambdaQueryWrapper<AcademicYear> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<AcademicYear> buildQueryWrapper(AcademicYearBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<AcademicYear> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getAcademicYear()), AcademicYear::getAcademicYear, bo.getAcademicYear());
        lqw.eq(bo.getAcademicName() != null, AcademicYear::getAcademicName, bo.getAcademicName());
        lqw.eq(bo.getStatus() != null, AcademicYear::getStatus, bo.getStatus());
        lqw.orderByAsc(AcademicYear::getAcademicYear);
        return lqw;
    }

    /**
     * 新增学年管理
     */
    @Override
    public Boolean insertByBo(AcademicYearBo bo) {
        AcademicYear add = MapstructUtils.convert(bo, AcademicYear.class);
        AcademicYearVo academicYearVo1 = baseMapper.selectAcademicYearVo(new QueryWrapper<>());
        if(ObjectUtils.isEmpty(academicYearVo1)){
            add.setAcademicYear("0");
        }else {
            int i = Integer.parseInt(academicYearVo1.getAcademicYear());
            Integer key=i+1;
            add.setAcademicYear(key.toString());
        }
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改学年管理
     */
    @Override
    public Boolean updateByBo(AcademicYearBo bo) {
        AcademicYear update = MapstructUtils.convert(bo, AcademicYear.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(AcademicYear entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除学年管理
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public Boolean switchAcademic(AcademicYearBo bo) {
        AcademicYearVo academicYearVo = this.queryById(bo.getId());
        Long academicKey=0L;
        AcademicYearVo academicYearVo1 = baseMapper.selectAcademicYearVo(new QueryWrapper<>());
        if(ObjectUtils.isNotEmpty(academicYearVo)&&"0".equals(bo.getFlag())){
            if(ObjectUtils.isNotEmpty(academicYearVo1)&&academicYearVo.getAcademicYear().equals(academicYearVo1.getAcademicYear())){
                throw new ServiceException("已无下一学年，切换下一学年失败");
            }
            academicKey = Long.parseLong(academicYearVo.getAcademicYear()) + 1L;
        }else if(ObjectUtils.isNotEmpty(academicYearVo)&&"1".equals(bo.getFlag())){
            if("0".equals(academicYearVo.getAcademicYear())){
                throw new ServiceException("已无上一学年，切换上一学年失败");
            }
            academicKey = Long.parseLong(academicYearVo.getAcademicYear()) - 1L;
        }
        AcademicYearVo taskInfoVo = baseMapper.selectVoOne(
            new LambdaQueryWrapper<AcademicYear>()
                .eq(AcademicYear::getAcademicYear, academicKey.toString())
        );
        AcademicYear academicYearUp=new AcademicYear();
        academicYearUp.setId(taskInfoVo.getId());
        academicYearUp.setStatus(1L);
        Boolean b= baseMapper.updateById(academicYearUp) > 0;
        if(!b){
            throw new ServiceException("切换学年失败");
        }
        academicYearUp.setId(bo.getId());
        academicYearUp.setStatus(0L);
        Boolean b1= baseMapper.updateById(academicYearUp) > 0;
        if(!b1){
            throw new ServiceException("切换学年失败");
        }
        return true;
    }
}
